const mysql = require('mysql')
const config = require('../config')
/**
 * @description 数据库连接
 * @type {{connect: (function(*=): Promise<any>)}}
 */
module.exports = {
	connect: function (sql) {
		return new Promise(function (resolve, reject) {
	        const connection = mysql.createConnection({
	            host     : config.MYSQL_CONNECT_CONFIG.HOSTNAME,
	            user     : config.MYSQL_CONNECT_CONFIG.USERNAME,
	            password : config.MYSQL_CONNECT_CONFIG.PASSWORD,
	            database : config.MYSQL_CONNECT_CONFIG.DATABASE
	        })
			/**
			 * @description 连接
			 */
	        connection.connect(function(err){
	            if (err){
	                reject(err)
	            }
	        })
			/**
			 * @description 查询
			 */
	        connection.query(sql, function (error, result) {
	            if (error) {
	                reject(error)
	            } else {
	                resolve(result)
	            }
	        })
			/**
			 * @description 连接关闭
			 */
	        connection.end(function(err){
	            if (err){
	                reject(err)
	            }
	        })
	    })
	}
}
